package com.tuzhi.sys.web.controller;

import com.tuzhi.base.redis.MyRedisTemplate;
import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.base.web.vo.Result;
import com.tuzhi.common.domain.ResultBO;
import com.tuzhi.config.ConstantConfig;
import com.tuzhi.sys.jpa.entity.SysDictionaryEntity;
import com.tuzhi.sys.service.SysDictionaryService;
import com.tuzhi.util.StringTzUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 功能说明：
 *
 * @author abu
 * @version 1.1.0
 * @date 2023/3/31
 */
@Slf4j
@RestController
@Api(tags = "查询系统配置")
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    MyRedisTemplate redisCache;

    @Autowired
    SysDictionaryService sysDictionaryService;

    @ApiOperation(value = "获取报价系统版本")
    @GetMapping(value = {"/getBillVersions"})
    public AjaxResultVO getBillVersions() {
        try {
            Integer billVersion = redisCache.get(ConstantConfig.billVersionKey);
            if (billVersion == null || billVersion.intValue() == 0) {
                billVersion = 1;
                redisCache.set(ConstantConfig.billVersionKey, 1);
            }
            AjaxResultVO ajaxResultVO = AjaxResultHelper.success();
            ajaxResultVO.setData(billVersion);
            return ajaxResultVO;
        } catch (Exception ex) {
            AjaxResultVO ajaxResultVO = AjaxResultHelper.error();
            ajaxResultVO.setData(null);
            return ajaxResultVO;
        }
    }

    @GetMapping(value = {"/updateBillVersions"})
    public AjaxResultVO updateBillVersions() {
        try {

            redisCache.incr(ConstantConfig.billVersionKey);

            Integer billVersion = redisCache.get(ConstantConfig.billVersionKey);
            if (billVersion == null || billVersion.intValue() == 0) {
                billVersion = 1;
                redisCache.set(ConstantConfig.billVersionKey, 1);
            }
            AjaxResultVO ajaxResultVO = AjaxResultHelper.success();
            ajaxResultVO.setData(billVersion);
            return ajaxResultVO;
        } catch (Exception ex) {
            AjaxResultVO ajaxResultVO = AjaxResultHelper.error();
            ajaxResultVO.setData(null);
            return ajaxResultVO;
        }
    }

    @GetMapping(value = {"/getFineRecVersions"})
    public AjaxResultVO getFineRecVersions() {
        try {
            String version = redisCache.get(ConstantConfig.Fine_Recognition_VersionKey);
            AjaxResultVO ajaxResultVO = AjaxResultHelper.success();
            ajaxResultVO.setData(version);
            return ajaxResultVO;
        } catch (Exception ex) {
            AjaxResultVO ajaxResultVO = AjaxResultHelper.error();
            ajaxResultVO.setData(null);
            return ajaxResultVO;
        }
    }

    @PutMapping(value = {"/updateFineRecVersion/{version}"})
    public AjaxResultVO updateFineRecVersion(@PathVariable("version") String version) {
        try {
            redisCache.set(ConstantConfig.Fine_Recognition_VersionKey, version);
            AjaxResultVO ajaxResultVO = AjaxResultHelper.success();
            ajaxResultVO.setData(version);
            return ajaxResultVO;
        } catch (Exception ex) {
            AjaxResultVO ajaxResultVO = AjaxResultHelper.error();
            ajaxResultVO.setData(null);
            return ajaxResultVO;
        }
    }

    @GetMapping(value = {"/getNotAuthUrl"})
    public Set<String> getNotAuthUrl() {
        try {
            Set<String> notAuthUrl = redisCache.getSet(ConstantConfig.getNotAuthUrlRedisKey());
            if (notAuthUrl != null && !notAuthUrl.isEmpty()) {
                return notAuthUrl;
            } else {
                List<SysDictionaryEntity> list = sysDictionaryService.listEntityOfDicName("notAuthUrl");
                if (list != null && !list.isEmpty()) {
                    for (SysDictionaryEntity item : list) {
                        notAuthUrl.add(item.getDicValue());
                    }
                    redisCache.setSet(ConstantConfig.getNotAuthUrlRedisKey(), notAuthUrl);
                }
                return notAuthUrl;
            }
        } catch (Exception ex) {

            log.error("加载不校验的URL资源出错。。{}", ExceptionUtils.getStackTrace(ex));
        }
        return null;
    }
}
